package com.mo.ld.activity.gate

import androidx.fragment.app.FragmentActivity
import com.dylanc.longan.activity
import com.mo.ld.databinding.ActGateBinding
import com.mo.lib.http.KCallBack
import com.mo.lib.ui.activity.BaseVbActivity
import com.mo.lib.utils.setLongClick
import com.mo.lib.view.recyclerview.layoutG
import okhttp3.Call
import org.litepal.LitePal

/**
 * @ author：mo
 * @ data：2022/4/14:11:15
 * @ 功能：
 */
class GateListActivity : BaseVbActivity<ActGateBinding>() {
    override fun initData() {
        //available
        viewBinding.krvCionListAvailable.layoutG(5).adapter = adapterAvailable

        //locked
        viewBinding.krvCionListLocked.layoutG(5).adapter = adapterLocked

        //buy
        viewBinding.krvCionListBuy.layoutG(5).adapter = adapterBuy

        //sell
        viewBinding.krvCionListSell.layoutG(5).adapter = adapterSell

        //collect
        viewBinding.krvAllCionCollection.layoutG(5).adapter = adapterCollect

        //all
        viewBinding.krvAllCion.layoutG(5).adapter = adapterAll //
        //        //tabs
        //        viewBinding.krvAllCionTab.layoutG(4).adapter = adapterTab
        //        adapterTab.setNewInstance(arrayListOf("重置", "筛选", "策略","btc"))
        viewBinding.tvAllCion.setLongClick {
            getPairs(object : KCallBack<List<String>>(activity as FragmentActivity) {
                override fun onSuccess(call: Call, result: List<String>) {
                    val temp = result.filter { it.split("_")[1] == "USDT" && !(it.contains("3")) && !(it.contains("5")) && !(it.contains("1")) && !(it.contains("8")) }.sorted()
                    LitePal.deleteAll(GateCoins::class.java)
                    for (item in temp) {
                        val db = GateCoins(item.split("_")[0])
                        db.save()
                    }
                    adapterAll.setNewInstance(LitePal.findAll(GateCoins::class.java))
                }
            })
        }

    }

    override fun onResume() {
        super.onResume()
        getBalances { av, lo ->
            run {
                adapterAvailable.setNewInstance(av)
                adapterLocked.setNewInstance(lo)
            }
        }
        getOpenOrders {
            adapterBuy.setNewInstance(this.orders.filter { it.type == "buy" }.toMutableList())
            adapterSell.setNewInstance(this.orders.filter { it.type == "sell" }.toMutableList())
        }

    }

    val adapterAvailable = getAdapterBalances(this)
    val adapterLocked = getAdapterBalances(this)
    val adapterBuy = getAdapterOpenOrders(this)
    val adapterSell = getAdapterOpenOrders(this)
    val adapterCollect = getAdapterCollectList(this)
    val adapterAll = getAdapterAllCionList(this, adapterCollect)
}
